package com.imooc.controller;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.google.gson.Gson;
import com.imooc.base.BaseInfoProperties;
import com.imooc.pojo.JobType;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.JobTypeService;
import com.imooc.utils.GsonUtils;
import com.imooc.vo.JobTypeSecondAndThirdVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 职位类型
 * @author liansen
 * @create 05-18-0:24
 */
@RequestMapping("/jobType")
@RestController
public class JobTypeController extends BaseInfoProperties {

    @Autowired
    private JobTypeService jobTypeService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //**********************用户端*************************

    /**
     * 查询一级职位类型
     * @return
     */
    @GetMapping("/app/initTopList")
    public GraceJSONResult initTopList(){
        String jobTypeJson = redisTemplate.opsForValue().get(TOP_JOB_TYPE_LIST);
        List<JobType> jobTypes;
        if(StringUtils.isNotBlank(jobTypeJson)){
            jobTypes = GsonUtils.stringToList(jobTypeJson, JobType.class);
        }else {
            jobTypes = jobTypeService.getFatherData("0");
            redisTemplate.opsForValue().set(TOP_JOB_TYPE_LIST, new Gson().toJson(jobTypes));
        }
        return GraceJSONResult.ok(jobTypes);
    }

    /**
     * 通过一级节点id查询二级和三级节点数据 (通过代码实现)
     * @param id
     * @return
     */
    @GetMapping("/app/getSecondAndThirdListByTop/{id}")
    public GraceJSONResult getSecondAndThirdListByTop(@PathVariable("id") String id){
        String str = redisTemplate.opsForValue().get(SECOND_THIRD_JOB_TYPE_LIST + ":TopId:" + id);
        List<JobTypeSecondAndThirdVO> list;
        if(StringUtils.isNotBlank(str)){
            list = GsonUtils.stringToList(str, JobTypeSecondAndThirdVO.class);
        }else {
            list = jobTypeService.getSecondAndThirdListByTop(id);
            if(CollectionUtils.isEmpty(list)){
                redisTemplate.opsForValue().set(SECOND_THIRD_JOB_TYPE_LIST + ":TopId:" + id, "[]", 10 *60, TimeUnit.SECONDS);
            }else {
                redisTemplate.opsForValue().set(SECOND_THIRD_JOB_TYPE_LIST + ":TopId:" + id, new Gson().toJson(list));
            }
        }
        return GraceJSONResult.ok(list);
    }


    /**
     * 通过一级节点id查询二级和三级节点数据 (通过sql实现)
     * @param id
     * @return
     */
    @GetMapping("/app/getSecondAndThirdListByTopSQL/{id}")
    public GraceJSONResult getSecondAndThirdListByTopBySQL(@PathVariable("id") String id){
        List<JobTypeSecondAndThirdVO> list = jobTypeService.getSecondAndThirdListByTopBySQL(id);
        return GraceJSONResult.ok(list);
    }

    //**********************运营端*************************

    /**
     * 添加职位类型
     */
    @PostMapping("/createNode")
    public GraceJSONResult createNode(@RequestBody JobType jobType){
        //判断职位类别名称是否已经存在
        if(jobTypeService.getJobTypeExistByName(jobType.getName())){
            GraceJSONResult.errorMsg("职位名称已存在");
        }
        jobTypeService.createNode(jobType);
        return GraceJSONResult.ok();
    }


    /**
     * 查询一级职位类型数据
     */
    @GetMapping("/children/{id}")
    public GraceJSONResult getOneLevelData(@PathVariable("id") String id){
        List<JobType> jobTypes = jobTypeService.getFatherData(id);
        return GraceJSONResult.ok(jobTypes);
    }

    @PostMapping("/updateNode")
    public GraceJSONResult updateNode(@RequestBody JobType jobType){
        jobTypeService.updateById(jobType);
        return GraceJSONResult.ok();
    }

    @DeleteMapping("/deleteNode/{id}")
    public GraceJSONResult deleteNode(@PathVariable("id") String id){
        //如果该类型下面有子类型或者子子类型，就不能删除
        JobType jobType = jobTypeService.getById(id);
        if(Objects.isNull(jobType)){
            return GraceJSONResult.errorMsg("请选择正确的数据");
        }
        Long childrenCount = jobTypeService.getChildrenCount(jobType.getId());
        if(childrenCount > 0){
            return GraceJSONResult.errorMsg("该节点下有子节点, 无法删除");
        }
        jobTypeService.removeById(id);
        return GraceJSONResult.ok();
    }

}
